package cn.loveq.android.util;

import android.graphics.Color;
import kj.dsp.KJDigitalSignalProcessingAudioDataConsumer;
import kj.dsp.KJDigitalSignalProcessor;
import kj.dsp.KJFFT;

/**
 * 
 *    
 * 项目名称：loveq4Weibo   
 * 类名称：AudioChart   
 * 类描述：   频谱工具
 * 创建人：李伟豪   
 * 创建时间：2011-12-30 下午07:09:10     
 * @version    
 *
 */
public class AudioChart implements KJDigitalSignalProcessor {

	public static final int DEFAULT_FPS = 20;

	//FFT频谱分析仪默认样本量
	public static final int DEFAULT_SPECTRUM_ANALYSER_FFT_SAMPLE_SIZE = 512;

	//默认的频谱分析仪波段数目
	public static final int DEFAULT_SPECTRUM_ANALYSER_BAND_COUNT = 19;
	public static final float DEFAULT_SPECTRUM_ANALYSER_DECAY = 0.05f;
	public static final int DEFAULT_SPECTRUM_ANALYSER_PEAK_DELAY = 20;
	public static final float DEFAULT_SPECTRUM_ANALYSER_PEAK_DELAY_FPS_RATIO = 0.4f;
	public static final float DEFAULT_SPECTRUM_ANALYSER_PEAK_DELAY_FPS_RATIO_RANGE = 0.1f;
	public static final float MIN_SPECTRUM_ANALYSER_DECAY = 0.02f;
	public static final float MAX_SPECTRUM_ANALYSER_DECAY = 0.08f;
	public static final Color DEFAULT_BACKGROUND_COLOR = null;
	public static final Color DEFAULT_SCOPE_COLOR = null;
	public static final float DEFAULT_VU_METER_DECAY = 0.02f;

	// -- 光谱分析仪变量.
	//FFT 快速傅立叶变换算法
	private KJFFT fft;
	//旧的FFT
	private float[] old_FFT;
	//FFT样本大小
	private int saFFTSampleSize;
	//波段
	private int saBands;
	private float saColorScale;
	//SA增值器
	private float saMultiplier;
	private float saDecay = DEFAULT_SPECTRUM_ANALYSER_DECAY;
	private float sad;
	//private SourceDataLine m_line = null;
	//----------------------------------
	
	 // -- VU Meter
    private float oldLeft;
    private float oldRight;
    //  private float vuAverage;
    //  private float vuSamples;
    private float vuDecay = DEFAULT_VU_METER_DECAY;
    private float vuColorScale;
    // -- FPS calulations.
    private long lfu = 0;
    private int fc = 0;
    private boolean showFPS = false;
    private int fp = 0;
	//---------------------------
    
	//峰值
	private int[] peaks = new int[DEFAULT_SPECTRUM_ANALYSER_BAND_COUNT];
	//峰值延迟
	private int[] peaksDelay = new int[DEFAULT_SPECTRUM_ANALYSER_BAND_COUNT];

	//数字信号处理音频数据
	private KJDigitalSignalProcessingAudioDataConsumer dsp = null;
	

	public AudioChart() {
		initialize();
	}

	/**
	 * 
	 * initialize(初始化)    
	 */
	private void initialize() {
		setSpectrumAnalyserBandCount(DEFAULT_SPECTRUM_ANALYSER_BAND_COUNT);
		setSpectrumAnalyserFFTSampleSize(DEFAULT_SPECTRUM_ANALYSER_FFT_SAMPLE_SIZE);
	}

	/**
	* 设置编号的乐团频谱分析仪输出。
	*/
	public synchronized void setSpectrumAnalyserBandCount(int pCount) {
		saBands = pCount;
		peaks = new int[saBands];
		peaksDelay = new int[saBands];
		computeSAMultiplier();
	}

	/**
	 * 样本大小设置FFT只是计算频谱分析仪value。缺省值是512。
	 * @param pSize Cannot be more than the size of the sample provided by the DSP.
	 */
	public synchronized void setSpectrumAnalyserFFTSampleSize(int pSize) {
		saFFTSampleSize = pSize;
		fft = new KJFFT(saFFTSampleSize);
		old_FFT = new float[saFFTSampleSize];
		computeSAMultiplier();
	}

	/**
	 * 计算SA增值器
	 * */
	private void computeSAMultiplier() {
		saMultiplier = (saFFTSampleSize / 2) / saBands;
	}

	@Override
	public void process(float[] arg0, float[] arg1, float arg2) {

	}

}
